package com.flymobi.mshop.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.LinearLayout;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

import com.flymobi.mshop.R;

public class TagGalleryView extends LinearLayout implements
		OnItemSelectedListener {
	private TextView[] tag = new TextView[4];
	private Gallery gallery;

	public void setAdapter(SpinnerAdapter adapter) {
		gallery.setAdapter(adapter);
	}

	/**
	 * @return the gallery
	 */
	public Gallery getGallery() {
		return gallery;
	}

	/**
	 * @param gallery
	 *            the gallery to set
	 */
	public void setGallery(Gallery gallery) {
		this.gallery = gallery;
	}

	public TagGalleryView(Context context) {
		super(context);
		View v=LayoutInflater.from(context).inflate(R.layout.taggalleryview, null);
		tag[0] = (TextView) v.findViewById(R.id.tag1);
		tag[1] = (TextView) v.findViewById(R.id.tag2);
		tag[2] = (TextView) v.findViewById(R.id.tag3);
		tag[3] = (TextView) v.findViewById(R.id.tag4);
		gallery = (Gallery) v.findViewById(R.id.gallery);
		gallery.setOnItemSelectedListener(this);
		this.addView(v);
	}

	public TagGalleryView(Context context, AttributeSet attrs) {
		super(context, attrs);
		View v=LayoutInflater.from(context).inflate(R.layout.taggalleryview, null);
		tag[0] = (TextView) v.findViewById(R.id.tag1);
		tag[1] = (TextView) v.findViewById(R.id.tag2);
		tag[2] = (TextView) v.findViewById(R.id.tag3);
		tag[3] = (TextView) v.findViewById(R.id.tag4);
		gallery = (Gallery) v.findViewById(R.id.gallery);
		gallery.setOnItemSelectedListener(this);
		this.addView(v);
	}

	public void onItemSelected(AdapterView<?> parent, View view, int position,
			long id) {
		position = position % 4;
		setActive(position);
	}

	public void onNothingSelected(AdapterView<?> parent) {

	}

	private void setActive(int idx) {
		for(TextView t:tag){
			t.setBackgroundColor(0xff00aaff);
		}
		tag[idx].setBackgroundColor(0xffff0000);
	}
}
